package database

import (
	"github.com/go-xorm/xorm"
	_ "github.com/lib/pq"
	"os"
	"goblog/system"
	"fmt"
	"log"
)

var Orm *xorm.Engine

func init() {
	path, _ := os.Getwd()
	xx := path + "/conf/db.yml"
	var dbConf system.DBConf
	dbConf.GetDBConf(xx)
	var err error
	psqlInfo := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable", dbConf.Host, dbConf.Port, dbConf.User, dbConf.Password, dbConf.DbName)
	//格式
	Orm, err = xorm.NewEngine("postgres", psqlInfo)
	if err != nil {
		log.Fatal(err)
	}
	Orm.ShowSQL(true) //菜鸟必备
	//设置连接池的空闲数大小
	Orm.SetMaxIdleConns(5)
	//设置最大打开连接数
	Orm.SetMaxOpenConns(5)
	//设置schema
	Orm.SetSchema("goblog")

	err = Orm.Ping()
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println("connect postgresql success")

}